function extend(obj, source){
  for (var item in source){
    obj[item] = source[item];
  }
  return obj
};

(function (win, doc) {

  win.$$ = {
    setCookie: function (name, value) {
      var Days = 30;
      var exp = new Date();
      exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
      doc.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    },
    getCookie: function (name) {
      var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
      if (arr = doc.cookie.match(reg)) {
        return unescape(arr[2]);
      } else {
        return null;
      }
    },
    delCookie: function (name) {
      var exp = new Date();
      exp.setTime(exp.getTime() - 1);
      var cval = this.getCookie(name);
      if (cval != null) {
        doc.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
      }
    },
    ajax: function (configs) {
      var options = {
        'type': configs.type,
        'url': configs.url,
        'data': configs.data,
        'success': null,
        'error': null
      };
      var opts = extend(options, configs);
      var obj = new XMLHttpRequest();  // XMLHttpRequest对象用于在后台与服务器交换数据
      obj.open(options.type || 'GET', options.url, true);
      if (options.type == 'POST' || options.type == 'post') {
        obj.setRequestHeader("Content-type", "application/json"); // 发送信息至服务器时内容编码类型
        obj.send(JSON.stringify(opts.data));
      } else {
        obj.send(null);
      }
      obj.onreadystatechange = function () {
        if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) {  // 304未修改
          opts.success.call(this, obj.responseText);
        }
      };
    }
  };

  // 替换浏览器默认 alert, confirm 弹窗:  
  win.createPop = function (config) {
    var pop = doc.createElement('div'),
      overlayer = doc.createElement('div'),
      layerPop = doc.createElement('div'),
      layerContent = doc.createElement('div'),
      layerTit = doc.createElement('h3'),
      layerBtns = doc.createElement('div'),
      textnode = doc.createTextNode(config.msg),
      yes = doc.createElement('button'),
      yestext = doc.createTextNode('确定'),
      no = doc.createElement('button'),
      notext = doc.createTextNode('取消');
    
    pop.className = 'layer';
    overlayer.className = 'overlayer';

    layerTit.className = 'layerTit';

    layerContent.className = 'layerContent';
    layerContent.appendChild(textnode);

    layerBtns.className = 'layerBtns';

    yes.className = 'popBtn btn_ok';
    yes.setAttribute('type', 'button');
    yes.appendChild(yestext);
    layerBtns.appendChild(yes);

    if (config.type == 'confirm') {
      pop.classList.add('confirmPop');
      no.className = 'popBtn btn_cancel';
      no.setAttribute('type', 'button');
      no.appendChild(notext);
      layerPop.appendChild(no);
      layerBtns.appendChild(no);
      title = doc.createTextNode('请确认');
    } else if (config.type == 'alert') {
      pop.classList.add('alertPop');
      title = doc.createTextNode('高能预警');
    }
    
    layerTit.appendChild(title);
    layerPop.className = 'layerPop';
    layerPop.appendChild(layerTit);
    layerPop.appendChild(layerContent);
    layerPop.appendChild(layerBtns);
    pop.appendChild(overlayer);
    pop.appendChild(layerPop);
    $('body')[0].appendChild(pop);

    //点击事件
    yes.onclick = function () {
      config.yesHandle && config.yesHandle();
      $('body')[0].removeChild(pop);
    };
    no.onclick = function () {
      config.noHandle && config.noHandle();
      $('body')[0].removeChild(pop);
    };
  };

  win.alert = function (config) {
    this.createPop({
      'type': 'alert',
      'msg': config.msg,
      'yesHandle':config.cb
    });
  };

  win.confirm = function (config) {
    this.createPop({
      'type': 'confirm',
      'msg': config.msg,
      'yesHandle': config.yesHandle,
      'noHandle': config.noHandle
    });
  };

  //初始化页面
  if ($('#wrapper').length>0) {
    setTimeout(function(){
      $('#wrapper')[0].classList.add('loaded');
    }, 100);
  }
  
  $(doc).on('click', '.label.checkbox', function () {
    $(this).toggleClass('on');
  });

  $(doc).on('click', '.label.radio', function () {
    if (!$(this).hasClass('on')){
      $(this).addClass('on').siblings('.radio').removeClass('on');
    }
    
  });

})(window, document);

